$(function () {
    var amend_datagrid = $("#amend_datagrid")
    var amend_dialog = $("#amend_dialog")
    var insure_Form = $("#insure_Form")
    var product_dialog = $("#product_dialog")
    var product_datagrid = $("#product_datagrid")
    var productItem_datagrid = $("#productItem_datagrid")
    amend_dialog.dialog({
        closed: true,
        title: '编辑保单',
        width: 700,
        height: 600,
        buttons: '#edit_buttons'
    })
    amend_datagrid.datagrid({
        url: "/temporary/queryAmend.do",
        fitColumns: true, // 自适应列
        fit: true,
        striped: true,  // 斑马线
        pagination: true,
        pageSize: 10,
        pageNumber: 1,
        singleSelect: true, // 单选
        toolbar: "#amend_toolbar",
        columns: [
            [
                {field: 'id', hidden: true},
                {field: 'sn', title: '保单编号', width: 80},
                {
                    field: 'client', title: '投保人', width: 80, formatter: function (value, row) {
                        if (row.client) {
                            return row.client.name
                        }
                    }
                },
                {field: 'applyDate', title: '申请日期', width: 80},
                {
                    field: 'time', title: '保险时长', width: 80
                },
                {
                    field: 'inputUser', title: '业务员', width: 80, formatter: function (value, row) {
                        if (row.inputUser) {
                            return row.inputUser.username
                        }
                    }
                },
                {field: 'totalAmount', title: '投保总金额', width: 80},
                {
                    field: 'item', title: '批改意见', width: 80, formatter: function () {
                        return '<a class="showOpinion">查看批改意见</a>';
                    }
                }
            ]]
    })
    $("#myDiv").on("click", "a.showOpinion", function () {
        //获取当前选中的行
        var row = amend_datagrid.datagrid('getSelected');
        $.messager.alert('批改意见', row.opinion)
    })
    var objMethod = {
        query: function () {
            //获取高级查询的参数值
            var clientName = $("#clientName").textbox('getValue')
            var sn = $("#sn").textbox('getValue')
            //重新加载数据表格
            amend_datagrid.datagrid('load', {
                clientName: clientName,
                sn: sn
            });
        },
        edit: function () {
            //做回显
            var row = amend_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            // //回显保单id
            $("#id").val(row.id)
            //回显当前车辆信息
            $.get('/car/selectByInsureId.do', {insureId: row.id}, function (data) {
                $("#carId").val(data.id)
                $("#carOwnName").textbox('setValue', data.owner)
                $("#carNum").textbox('setValue', data.carNum)
                $("#buyDate").textbox('setValue', data.purchaseDate)
                $("#brand").textbox('setValue', data.brand)
                $("#model").textbox('setValue', data.productName)
                $("#exhaust").textbox('setValue', data.displacement)
                $("#price").textbox('setValue', data.evaluation)
                $("#carClass").combobox('setValue', data.type)
                $("#size").combobox('setValue', data.size)
            })
            //回显保险时长
            $("#burningTime").textbox('setValue', row.time)
            //回显总金额
            $("#totalAmount").html(row.totalAmount)
            //回显明细表格
            $.get('/product/selectAllProduct.do', {insureId: row.id}, function (data) {
                productItem_datagrid.datagrid('loadData', data);
                $("#organization_combogrid").combogrid('setValue', data[0].organization.name)
                product_datagrid.datagrid('load', {
                    organizationId: data[0].organization.id
                })
            })
            amend_dialog.dialog('open')
        },
        save: function () {
            //提交表单
            insure_Form.form('submit', {
                url: '/insureInfo/saveOrUpdate.do',
                onSubmit: function (params) {
                    var rows = productItem_datagrid.datagrid('getRows')
                    $.each(rows, function (index, value) {
                        params["products[" + index + "].id"] = value.id
                        params["products[" + index + "].annualFee"] = value.annualFee
                    })
                },
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('友情提示', '修改成功', 'info', function () {
                            //关闭对话框
                            amend_dialog.dialog('close')
                            //刷新数据表格
                            amend_datagrid.datagrid('reload')
                        });
                    } else {
                        $.messager.alert("友情提示", data.msg, "warning")
                    }
                }
            })
        },
        close: function () {
            amend_dialog.dialog('close')
        },
        refresh: function () {
            //刷新数据表格
            amend_datagrid.datagrid('reload')
        },
        audit: function () {
            var row = amend_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            $.get('/temporary/changeState.do', {id: row.id, state: 2}, function (data) {
                if (data.success) {
                    $.messager.alert('友情提示', '提交成功,请及时提醒管理员审核', 'info', function () {
                        //刷新数据表格
                        amend_datagrid.datagrid('reload')
                    });
                } else {
                    $.messager.alert("友情提示", data.msg, "warning")
                }
            })
        }
    }
    // 绑定事件
    $('a[data-cmd]').click(function () {
        objMethod[$(this).data('cmd')]();
    })
    $("#add_linkbutton").linkbutton({
        iconCls: 'icon-add',
        onClick: function () {
            //判断有没有选中数据
            if ($("#organization_combogrid").val() == '请选择') {
                $.messager.alert("友情提示", "亲, 请先选择承保机构!", "info")
                return
            }
            product_dialog.dialog('open')
        }
    })
    product_dialog.dialog({
        closed: true,
        title: '选择保险产品',
        width: 700,
        height: 500,
        buttons: "#product_buttons",
        onclose: function () {
            product_datagrid.datagrid('loadData ', [])
        }
    })
    product_datagrid.datagrid({
        url: '/product/query.do',
        pagination: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        fit: true,
        columns: [[
            {field: 'id', hidden: true},
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'productName', title: '保险名称', width: 100,},
            {
                field: 'organization', title: '保险机构', width: 100, formatter: function (value, row, index) {
                    return value.name;
                }
            },
            {field: 'period', title: '保障年限', width: 100, align: 'right'},
            {field: 'amount', title: '保额', width: 100, align: 'right'},
            {field: 'annualFee', title: '基本年费', width: 100, align: 'right'},
            {
                field: 'inSale', title: '销售状态', width: 100, align: 'right', formatter: function (value, row, index) {
                    if (value) {
                        return '<font color="blue">在售</font>'
                    } else {
                        return '<font color="red">停售</font>'
                    }
                }
            },
            {
                field: 'noCompensate',
                title: '不计免赔',
                width: 100,
                align: 'right',
                formatter: function (value, row, index) {
                    if (value) {
                        return '<font color="blue">是</font>'
                    } else {
                        return '<font color="red">否</font>'
                    }
                }
            }
        ]]
    })
    $("#organization_combogrid").combogrid({
        panelWidth: 450,
        idField: 'id',
        textField: 'name',
        url: '/organization/selectOrganizationInfo.do',
        columns: [[
            {field: 'id', title: '编号', width: 60},
            {field: 'sn', title: '机构代码', width: 100},
            {field: 'name', title: '机构名称', width: 120},
            {field: 'legalPerson', title: '法人代表', width: 100}
        ]],
        onSelect: function (index, row) {
            product_datagrid.datagrid('load', {
                organizationId: row.id
            })
            $("#organizationId").val(row.id)
            //清空明细表格
            productItem_datagrid.datagrid('loadData', [])
            //把总额设置为0
            $("#totalAmount").html(0)
            //把年限设置为空
            $("#burningTime").textbox('clear')
        }

    })
    productItem_datagrid.datagrid({
        //url:'/product/selectAllProduct.do',
        //fit:true,
        fitColumns: true,
        toolbar: "#productItem_buttons",
        columns: [[
            {field: 'id', hidden: true},
            {field: 'productName', title: '保险产品', width: 100},
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'annualFee', title: '保险年费', width: 100},
        ]],
    })
    $("#product_ok").linkbutton({
        onClick: function () {
            //判断是否选中行
            var row = product_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("友情提示", "至少选中一行数据", "info")
                return;
            }
            //判断选中行在数据表格中是否存在
            //获取表格中的所有行
            var rows = productItem_datagrid.datagrid("getRows");
            var ids = $.map(rows, function (p) {
                return p.id
            })
            if ($.inArray(row.id, ids) == -1) {
                productItem_datagrid.datagrid("appendRow", row)
            }
            //把总额设置为0
            $("#totalAmount").html(0)
            //把年限设置为空
            $("#burningTime").textbox('clear')
            //关闭窗口
            product_dialog.dialog('close')
        }
    })
    $("#product_remove").linkbutton({
        onClick: function () {
            //获取数据表格中选中的行
            //判断是否选中行
            var row = productItem_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert("友情提示", "至少选中一行数据", "info")
                return;
            }
            //获得选中行的索引
            var index = productItem_datagrid.datagrid('getRowIndex', row);
            //删除指定索引的行
            productItem_datagrid.datagrid('deleteRow', index)
            //把总额设置为0
            $("#totalAmount").html(0)
            //把年限设置为空
            $("#burningTime").textbox('clear')
        }
    })
    //保险时长
    $("#burningTime").textbox({
        width: 60,
        required: true,
        onChange: function (newValue, oldValue) {
            //获取数据表格中的所有的行
            var rows = productItem_datagrid.datagrid('getRows');
            //获取所有的基础年费
            var totalAmount = 0;
            $.each(rows, function (i, val) {
                totalAmount += val.annualFee
                console.log(totalAmount);
            })
            totalAmount = totalAmount * newValue
            //把总价设置进span
            $("#totalAmount").html(totalAmount)
        }
    })
})